import { defineStore } from 'pinia';
import * as api from '../api/vip';
import routes from '../router/dynamicRouter';
//引入封装的动态路由函数
import recursionRoutes from '../router/recursionRoutes';
//引入动态路由
import remove from '../router/remove';
//引入静态路由
import staticRoute from '../router/staticRoute';
//引入路由对象
import router from '../router';
export const usePermission = defineStore('permission', {
  state() {
    return {
      menuList: [],
      logOut: false
    };
  },
  actions: {
    fetch_menuList() {
      return api.getMenuApi().then((res) => {
        // console.log(res);
        // console.log(routes);
        let a = recursionRoutes(res.data.menuList, routes);
        // console.log(a);
        this.menuList = a;
        //开始动态添加路由
        //1.先从动态路由中,找到'/'的路由对象
        let target = remove.find((route) => route.path === '/');
        //2将用户路由变成他的孩子
        target.children = a;
        //3静态路由也要添加的remove动态路由的孩子里面
        target.children = [...target.children, ...staticRoute];
        //4使用addRoute方法,将remove添加到路由的routes配置中
        router.addRoute(target);
      });
    }
  }
});
